<?php
/**
 * 零起飞-(07FLY-CRM)
 * ==============================================
 * 版权所有 2015-2028   成都零起飞网络，并保留所有权利。
 * 网站地址: http://www.07fly.xyz
 * ----------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ==============================================
 * Workflowor: kfrs <goodkfrs@QQ.com> 574249366
 * Date: 2019-10-3
 */

namespace app\workflow\logic;

use think\Db;
use think\Request;

/**
 * workflow=》逻辑层
 */
class Workflow extends WorkflowBase
{
    /**
     * workflow列表
     * @param array $where
     * @param bool $field
     * @param string $order
     * @param int|mixed $paginate
     * @return
     */
    public function getWorkflowList($where = [], $field = true, $order = '', $paginate = DB_LIST_ROWS)
    {
        $list = $this->modelWorkflow->getList($where, $field, $order, $paginate);
        foreach ($list as &$row){
            $nodes=$this->modelWorkflowNode->getList(['workflow_id'=>$row['id']],'name,sort','sort asc',false);
            $row['nodes_name']='';
            $i=1;
            foreach ($nodes as $key2=>$row2){
                $row['nodes_name'] .='('.$i.'):'.$row2['name'].'; ';
                $i++;
            }
        }
        $list=$list->toArray();
        return $list;
    }

    /**
     * workflow添加
     * @param array $data
     * @return array
     */
    public function workflowAdd($data = [])
    {

        $validate_result = $this->validateWorkflow->scene('add')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateWorkflow->getError()];
        }
        $data['create_user_id']=SYS_USER_ID;
        $result = $this->modelWorkflow->setInfo($data);

        $result && action_log('新增', '新增workflow：' . $data['name']);
        $url = url('show');
        return $result ? [RESULT_SUCCESS, '添加成功', $url] : [RESULT_ERROR, $this->modelWorkflow->getError()];
    }

    /**
     * workflow编辑
     * @param array $data
     * @return array
     */
    public function workflowEdit($data = [])
    {

        $validate_result = $this->validateWorkflow->scene('edit')->check($data);
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateWorkflow->getError()];
        }
        $result = $this->modelWorkflow->setInfo($data);

        $url = url('show');
        $result && action_log('编辑', '编辑workflow，name：' . $data['name']);
        return $result ? [RESULT_SUCCESS, '编辑成功', $url] : [RESULT_ERROR, $this->modelWorkflow->getError()];
    }

    /**
     * workflow删除
     * @param array $where
     * @return array
     */
    public function workflowDel($where = [])
    {

        $result = $this->modelWorkflow->deleteInfo($where,true);

        $result && action_log('删除', '删除workflow，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelWorkflow->getError()];
    }

    /**workflow信息
     * @param array $where
     * @param bool $field
     * @return
     */
    public function getWorkflowInfo($where = [], $field = true)
    {
        return $this->modelWorkflow->getInfo($where, $field);
    }

    /**
     * 获取列表搜索条件
     */
    public function getWhere($data = [])
    {
        $where = [];
        //关键字查
        !empty($data['keywords']) && $where['name|content'] = ['like', '%' . $data['keywords'] . '%'];
        //下次联系时间
        if (!empty($data['next_rangedate'])) {
            $range_date = str2arr($data['next_rangedate'], "-");
            $where['next_time'] = ['between', $range_date];
        }
        return $where;
    }
    /**
     * 获取排序条件
     */
    public function getOrderBy($data = [])
    {
        $order_by = "";
        if (!empty($data['orderField'])) {
            $orderField = $data['orderField'];
            $orderDirection = $data['orderDirection'];
        } else {
            $orderField = "";
            $orderDirection = "";
        }
        if ($orderField == 'by_link') {
            $order_by = "link_time $orderDirection";
        } else if ($orderField == 'by_next') {
            $order_by = "next_time $orderDirection";
        } else if ($orderField == 'by_nodays') {
            $order_by = "nodays $orderDirection";
        } else {
            $order_by = "id desc";
        }
        return $order_by;
    }


    /**
     * 获得业务表=》关联流程模板
     *
     * Author: kfrs <goodkfrs@QQ.com> created by at 2020/10/28 0028
     */
    public function getBusinessWorkflow($business){
        $workflow_id=$this->modelWorkflow->getValue(['business_type'=>$business], 'id');
        if($workflow_id){
            return $workflow_id;
        }else{
            return 0;
        }
    }

}
